<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"/>
        <title>AJ MCP SDK/MCP Server SDK 工具（Tool）开发</title>
        <meta name="description" content="轻量级的 Java 8 Model Context Protocol (MCP) SDK. MCP Server SDK 工具（Tool）开发"/>
        <meta name="keywords" content="mcp, mcp sdk, java mcp, mcp client, mcp server, java8 mcp, 工具（Tool）开发"/>
        <meta name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" href="/asset/style/main.css"/>
        <link rel="icon" type="image/png" href="/asset/favicon.ico"/>
        <script src="/asset/common.js"></script>
    </head>
    <body>
        <nav>
            <div>
                <div class="links">
                    <a href="/">🏠 首页</a>
                    | ⚙️ 源码:
                    <a target="_blank" href="https://github.com/lightweight-component/aj-mcp">Github</a>/<a target="_blank" href="https://gitcode.com/lightweight-component/aj-mcp">Gitcode</a>
                    |
                    <a href="/">英文版本</a>
                </div>
                <h1>AJ MCP SDK</h1>
                <h3>用户手册</h3>
            </div>
        </nav>
        <div>
            <menu>
                
                <ul>
                    <li class="selected">
                        <a href="/cn">首页</a>
                    </li>
                    <li>
                        <a href="/architecture/index-cn">架构原理</a>
                    </li>
                </ul>
                <h3>MCP 客户端 SDK</h3>
                <ul>
                    <li>
                        <a href="/client/setup-client-cn">安装与配置</a>
                    </li>
                    <li>
                       <a href="/client/initialization-cn">初始化</a>
                    </li>
                    <li>
                       <a href="/client/resource-cn">Resources 用法</a>
                    </li>
                     <li>
                       <a href="/client/prompt-cn">Prompts 用法</a>
                    </li>
                     <li>
                       <a href="/client/tool-cn">Tools 用法</a>
                    </li>
                </ul>
                <h3>MCP 服务端 SDK</h3>
                <ul>
                      <li>
                           <a href="/server/usage-server-cn">Server SDK 用法指南</a>
                      </li>
                      <li>
                           <a href="/server/res-server-cn">Resources 用法</a>
                      </li>
                      <li>
                           <a href="/server/pro-server-cn">Prompts 用法</a>
                      </li>
                      <li>
                             <a href="/server/tool-server-cn">Tools 用法</a>
                       </li>
                      <li>
                         <a href="/server/sample-cn">整合例子</a>
                      </li>
                </ul>

                <h3>其他</h3>
                <ul>
                    <li><a href="/misc/versions">版本记录</a></li>
                    <li><a href="/misc/contact-cn">联系方式</a></li>
                </ul>
            </menu>
            <article>
                <h1>工具（Tool）开发</h1>
<p>在标记了 <code>@McpService</code> 注解的类中，通过为方法添加 <code>@Tool</code> 注解来创建工具。框架会自动发现这些方法，并将其暴露为可调用的工具。</p>
<pre><code class="language-java">@McpService
public class MyTools {
    @Tool(description = &quot;回显字符串&quot;)
    public String echoString(@ToolArg(description = &quot;要回显的字符串&quot;) String input) {
        return input;
    }

    @Tool(&quot;customName&quot;, description = &quot;自定义名称工具&quot;)
    public String myMethod() {
        return &quot;result&quot;;
    }
}
</code></pre>
<h2>参数处理与校验</h2>
<p>工具参数通过 @ToolArg 注解，提供用于校验和生成元数据的参数说明。每个参数可指定名称、描述及是否必填。</p>
<h3>参数注解</h3>
<table>
<thead>
<tr>
<th>属性</th>
<th>描述</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td>value</td>
<td>参数名（若为空则用方法参数名）</td>
<td>空字符串</td>
</tr>
<tr>
<td>description</td>
<td>面向客户端的参数描述</td>
<td>空字符串</td>
</tr>
<tr>
<td>required</td>
<td>是否为必填参数</td>
<td>true</td>
</tr>
</tbody>
</table>
<h2>类型映射</h2>
<p>框架自动将 Java 类型映射为 JSON Schema 类型：</p>
<table>
<thead>
<tr>
<th>Java 类型</th>
<th>JSON Schema 类型</th>
</tr>
</thead>
<tbody>
<tr>
<td>String, Character</td>
<td>&quot;string&quot;</td>
</tr>
<tr>
<td>int, long, float, double, Number</td>
<td>&quot;number&quot;</td>
</tr>
<tr>
<td>boolean, Boolean</td>
<td>&quot;boolean&quot;</td>
</tr>
<tr>
<td>其它类型</td>
<td>&quot;Object&quot;</td>
</tr>
</tbody>
</table>
<h2>工具实现示例</h2>
<h3>基础字符串工具</h3>
<pre><code class="language-java">@Tool(description = &quot;回显字符串&quot;)
public String echoString(@ToolArg(description = &quot;要回显的字符串&quot;) String input) {
    return input;
}
</code></pre>
<h3>无参数工具</h3>
<pre><code class="language-java">@Tool(description = &quot;耗时 10 秒完成&quot;)
public String longOperation() throws Exception {
    TimeUnit.SECONDS.sleep(5);
    return &quot;ok&quot;;
}
</code></pre>
<h3>返回内容工具</h3>
<pre><code class="language-java">@Tool(description = &quot;一张图片&quot;)
public ContentImage image() {
    String base64EncodedImage = ServerUtils.encodeImageToBase64(&quot;bird-eye.jpg&quot;);
    ContentImage content = new ContentImage();
    content.setMimeType(&quot;image/jpg&quot;);
    content.setData(base64EncodedImage);
    return content;
}
</code></pre>
<h3>多内容工具</h3>
<pre><code class="language-java">@Tool(description = &quot;列出全部内容&quot;)
public List&lt;Content&gt; getAll() {
        List&lt;Content&gt; list = new ArrayList&lt;&gt;();
        list.add(new ContentImage(/* ... */));
        list.add(new ContentText(&quot;Hello World&quot;));
        return list;
        }
</code></pre>

                <div id="comment-holder">
                </div>
            </article>
        </div>
        <footer>
             AJ-Util，开源框架 <a href="https://framework.ajaxjs.com" target="_blank">AJ-Framework</a> 的一部分。联系方式：
             frank@ajaxjs.com，<a href="https://blog.csdn.net/zhangxin09" target="_blank">作者博客</a>
             <br />
             <br />
             Copyright © 2025 Frank Cheung. All rights reserved.
         </footer>
    </body>
</html>